package com.ml.common.gateway.initBean;

import com.ml.common.gateway.utils.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.CommandLineRunner;

import java.util.Date;

/**
 * @author sqz
 * @Description: 做初始化bean的操作
 * @date 2019/3/22 10:30
 */
//@Component
//public class InitBean implements ApplicationRunner {
//public class InitBean implements InitializingBean {
public class InitBean implements CommandLineRunner {

    @Autowired
    private RedisClient redisClient;
    /**
     * 实现ApplicationRunner的方式
     * @throws Exception
     */
    //@Override
    public void run(ApplicationArguments args) throws Exception {
        Date date = new Date();
        for (int i = 0; i < 10000 ; i++) {
            redisClient.set("test"+i,String.valueOf(i));
        }
        System.out.println("耗时:"+(new Date().getTime()-date.getTime())/1000);
        Thread.sleep(10000);
    }

    /**
     * 实现InitializingBean的方式
     * @throws Exception
     */
    //@Override
    public void afterPropertiesSet() throws Exception {
        Date date = new Date();
        for (int i = 0; i < 10000 ; i++) {
            redisClient.set("test"+i,String.valueOf(i));
        }
        System.out.println("耗时:"+(new Date().getTime()-date.getTime())/1000);
        Thread.sleep(10000);
    }

    /**
     * 实现CommandLineRunner的方式
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        Date date = new Date();
        for (int i = 0; i < 10000 ; i++) {
            redisClient.set("test"+i,String.valueOf(i));
        }
        System.out.println("耗时:"+(new Date().getTime()-date.getTime())/1000);
        Thread.sleep(10000);
    }
}
